"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _system = require("../../static/utils/system.js");

var _system2 = _interopRequireDefault(_system);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {
    default: obj
  };
}

var app = getApp();
exports.default = Page({
  data: {
    isAuthorize: false,
    info: {
      avatar: "../../static/image/avatar.png",
      name: "门品学社",
      number: "0000",
      tname: "普通游客",
      deadline: "2000/01/01",
      delay: "已延期01天",
      dues: "0.00",
      smoney: "0.00",
      threshold: "0.00",
      integral: "0",
      tmoney: "0.00",
      tvalue: "0.00"
    }
  },
  onLoad: function onLoad() {},
  onShow: function onShow() {
    var _this = this;
    var openid = wx.getStorageSync("openid") || "";
    var phone = wx.getStorageSync('phone');
    var phoneinfo = wx.getStorageSync('phoneinfo');
    wx.getNetworkType({
      success: function success(res) {
        _this.setData({
          netWorkType: res.networkType
        });
      }
    });
    if (!phone) {
      var _this = this
      var host = "https://api.bjlya.com.cn/xcx/app.php?";
      var data = {
        mod: "do",
        act: "tips",
        ssid: wx.getStorageSync('openid') || ''
      };
      wx.request({
        url: host,
        data: data,
        method: "GET",
        header: {
          "content-type": "application/json"
        },
        success: function success(result) {
          // console.log(result)
          var info = result.data.info
          _this.setData({
            baseinfo: info.baseinfo
          })
        }
      })
    }
    if (!openid) {
      _this.setData({
        isAuthorize: true
      });
    } else {
      var data = {
        mod: "do",
        act: "info",
        ssid: openid
      };
      _system2.default.getUserInfo(data, "post");
      setTimeout(function() {
        _this.setData({
          openid: openid,
          info: wx.getStorageSync("info")
        });
      }, 1000);
    }
  },
  onHide: function onHide() {
    var _this = this;
    _this.setData({
      isAuthorize: false
    });
  },
  onShareAppMessage: function onShareAppMessage(res) {
    var _this = this;
    var userid = _this.data.info.id;
    return {
      title: "门品学社",
      desc: "当下最具人气的小程序！",
      imageUrl: "/static/image/icon.png",
      path: "/pages/online/online?id=" + userid
    };
  },
  refuse: function refuse() {
    var _this = this;
    _this.setData({
      isAuthorize: false
    });
    wx.switchTab({
      url: "/pages/online/online"
    });
  },
  login: function login(info) {
    var _this = this;
    var iv = info.detail.iv;
    var userInfo = info.detail.userInfo;
    var encryptedData = info.detail.encryptedData;
    if (userInfo) {
      wx.login({
        success: function success(response) {
          var code = response.code;
          var host = "https://api.bjlya.com.cn/xcx/app.php?";
          var data = {
            mod: "do",
            act: "openid",
            mid: code,
            iv: iv,
            encryptedData: encryptedData,
            cid: wx.getStorageSync("cid") || ""
          };
          wx.request({
            url: host,
            data: data,
            method: "GET",
            header: {
              "content-type": "application/json"
            },
            success: function success(result) {
              // console.log(result);
              wx.setStorageSync("type", result.data.info.type);
              wx.setStorageSync("openid", result.data.info.ssid);
              wx.setStorageSync("session_key", result.data.info.session);
              _this.setData({
                isAuthorize: false
              });
              var method = "post";
              var host = "https://api.bjlya.com.cn/xcx/app.php?";
              var data = {
                mod: "do",
                act: "info",
                ssid: result.data.info.ssid
              };
              if (method == "post") {
                wx.request({
                  url: host,
                  data: data,
                  method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  success: function success(res) {
                    wx.setStorageSync("info", res.data.info);
                    _this.setData({
                      info: res.data.info
                    })
                  }
                });
              } else if (method == "get") {
                wx.request({
                  url: host,
                  data: data,
                  method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                  header: {
                    "Content-Type": "application/json"
                  },
                  success: function success(res) {
                    wx.setStorageSync("info", res.data.info);
                    _this.setData({
                      info: res.data.info
                    })
                  }
                });
              }
            }
          });
        },
        fail: function fail(res) {
          wx.switchTab({
            url: "/pages/online/online"
          });
        }
      });
    } else {
      wx.switchTab({
        url: "/pages/online/online"
      });
    }
    wx.showToast({
      icon: "loading",
      duration: 1000,
      mask: false,
      success: function success() {
        setTimeout(function() {
          var pages = getCurrentPages();
          pages[pages.length - 1].onShow();
        }, 1000);
      }
    });
  },
  linkUserInfo: function linkUserInfo() {
    wx.navigateTo({
      url: "userInfo/userInfo"
    });
  },
  linkReward: function linkReward(e) {
    var money = e.currentTarget.dataset.money;
    wx.navigateTo({
      url: "reward/reward?money=" + money
    });
  },
  linkRecord: function linkRecord(e) {
    var _this = this;
    var prop = e.currentTarget.dataset.type;
    var mid = e.currentTarget.dataset.mid;
    wx.request({
      url: app.globalData.host,
      data: {
        mod: "do",
        act: "type",
        ssid: _this.data.openid,
        mid: mid
      },
      method: "post", // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function success(res) {
        //console.log(res);
        var header = JSON.stringify(res.data.info);
        wx.navigateTo({
          url: "record/record?type=" + prop + "&header=" + header + "&mid=" + mid
        });
      }
    });
  },
  linkSpread: function linkSpread() {
    wx.navigateTo({
      url: "spread/spread"
    });
  },
  linkSettings: function linkSettings() {
    wx.navigateTo({
      url: "settings/settings"
    });
  },
  linkPayService: function linkPayService(e) {
    var prop = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: "payService/payService?type=" + prop
    });
  }
});